using System;
using System.Linq;
using System.Collections.ObjectModel;
namespace FuzzyTaskList
{
    [Serializable]
    public class ActivityItem : BindableObject
    {
        public ActivityItem(FuzzyTaskList.FuzzyTaskItem taskRef, bool isComplete, DateTime activityDate)
        {
            _isTaskComplete = isComplete;
            ActivityDate = activityDate;
            TaskRef = taskRef;
        }

        private bool _isTaskComplete;
        public bool IsActivityComplete
        {
            get { return _isTaskComplete; }
            set
            {
                if (_isTaskComplete != value)
                {
                    if (value)
                    {
                        int index = FindInsertionPoint(TaskRef.History,ActivityDate);
                        TaskRef.History.Insert(index,ActivityDate);
                    }
                    else
                    {
                        TaskRef.History.Remove(ActivityDate);
                    }
                }
                _isTaskComplete = value;
                base.RaisePropertyChanged("IsActivityComplete");
                TaskRef.ComputeHistoryView();
            }
        }

        public ActivityStatus ActivityStatus
        {
            get
            {
                if (this.ActivityDate < TaskRef.StartDate)
                    return ActivityStatus.NotActive;

                DateTime lastactionDate = TaskRef.History.LastOrDefault(date => date <= ActivityDate);
                int numDaysFromLastAction = ActivityDate.Subtract(lastactionDate).Days;

                
                if (numDaysFromLastAction > TaskRef.MaxDays)
                {
                    return ActivityStatus.Late;
                }
                if (numDaysFromLastAction==0)
                {
                    return ActivityStatus.IsComplete;
                }
                if (numDaysFromLastAction < TaskRef.MaxDays && numDaysFromLastAction >= TaskRef.MinDays)
                {
                    return ActivityStatus.CanAct;
                }
                if (numDaysFromLastAction == TaskRef.MaxDays)
                {
                    return ActivityStatus.ShouldAct;
                }
                if (numDaysFromLastAction < TaskRef.MinDays)
                {
                    return ActivityStatus.TooSoon;
                }
                System.Diagnostics.Debug.Assert(false, "Unreachable code");
                return ActivityStatus.NotActive;
            }
        }


        private static int FindInsertionPoint(ObservableCollection<DateTime> list, DateTime newDate)
        {
            for (int ii = 0; ii < list.Count; ii++)
            {
                if (newDate < list[ii]  )
                {
                    return ii;
                }
            }
            return list.Count;
        }
        public DateTime ActivityDate { get; set; }
        public FuzzyTaskItem TaskRef { get; set; }
    }
}
